package whf.framework.web.tag;

import javax.servlet.jsp.JspException;

import whf.framework.util.BeanUtils;
import whf.framework.util.StringUtils;
import whf.framework.web.tag.ext.TagConstants;
import whf.framework.web.tag.ext.TagStructureException;

/**
 * tableTag的行模式扩展，与tabletag中的rowExt属性一样
 * @author wanghaifeng
 * @create Sep 2, 2006 4:56:28 PM
 * 
 */
public class TableRowExtTag extends HtmlTag{
	public String getOpenTag() {
		return super.getOpenTag(TagConstants.TAGNAME_CAPTION);
	}

	public String getCloseTag() {
		return super.getCloseTag(TagConstants.TAGNAME_CAPTION);
	}

	public int doStartTag() throws JspException {
		TableTag tableTag = (TableTag) findAncestorWithClass(this,
				TableTag.class);
		if (tableTag == null) {
			throw new TagStructureException(getClass(), "rowext", "table");
		} else {
			tableTag.setRowExt("true");
		}
		if (tableTag.isFirstIteration()) {
			return 2;
		}
		return 2;
	}

	public int doEndTag() throws JspException {
		TableTag tableTag = (TableTag) findAncestorWithClass(this,
				TableTag.class);
		if (tableTag == null) {
			throw new TagStructureException(getClass(), "rowext", "table");
		}
		if(this.getBodyContent() != null && !StringUtils.isEmpty(this.getBodyContent().getString()) && tableTag.getCurrentRow() != null){
			TableRow row = tableTag.getCurrentRow();
			String content = getBodyContent().getString();
			if(content.indexOf("$") >= 0){
				content = BeanUtils.replaceByProperty(row.getRowObject(), content);
			}
			tableTag.getCurrentRow().setRowExt(content);
		}
		this.release();
		return SKIP_BODY;
	}

	public void release() {
		super.release();
	}

}
